//
//  AppDelegate.swift
//  Microblog
//
//  Created by Roman on 17/1/4.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
//    //懒加载处理
//    lazy var picSelector: LWPictureSelectorController = {
//       let flowLayout = UICollectionViewFlowLayout()
//        //设置大小
//        flowLayout.itemSize = CGSize(width: selectoritemWidth, height: selectoritemWidth)
//        flowLayout.minimumInteritemSpacing = selectorCellMargin
//        //设置insert
//        flowLayout.sectionInset = UIEdgeInsetsMake(selectorCellMargin, selectorCellMargin, 0, selectorCellMargin)
//        let vc = LWPictureSelectorController(collectionViewLayout: flowLayout)
//        return vc
//    }()


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //注册通知
        regisgterNotification()
        //1.实例化window
        //2.设置跟视图控制器
        //3.设置window为主window
        window = UIWindow(frame: UIScreen.main.bounds)
        
       // window?.rootViewController = LWTabBarViewController()
        //直接这样定义控制器会导致系统崩溃
    
        
        //let nav = UINavigationController(rootViewController: LWComposeViewController())
       // window?.rootViewController = nav
        window?.rootViewController = defaultViewControll()

        
        window?.makeKeyAndVisible()
      
        window?.backgroundColor = UIColor.white
        
        return true
    }
    //注册通知
    private func regisgterNotification(){
        NotificationCenter.default.addObserver(self, selector: #selector(changeRootViewController(n:)), name: NSNotification.Name.AppChangRootVC, object: nil)
    }
    
    @objc private func changeRootViewController(n: Notification){
        
        //1.获取通知对象object 参数 -->控制器名称
        let vcName = n.object as! String
        print(vcName)
        //1.1 获取boundName 拼接类型名称
        let productName = Bundle.main.infoDictionary!["CFBundleName"] as! String
        let className = productName + "." + vcName
        
       // print(productName)
        
        //2.将字符串类型的控制器名称转换为类型
        let classType = NSClassFromString(className) as! UIViewController.Type
        //3.实例化控制器对象
        let rootVC = classType.init()
        //4.设置跟视图控制器
        window?.rootViewController = rootVC
    }
    
     //根据用户是否登录设置对应的根视图控制器
    private func defaultViewControll() -> UIViewController{
        return LWUserAccountViewModel.shard.userLogin ? LWWelcomViewController() : LWTabBarViewController()
    }
    

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    //移除通知 ,没有意义,但还是要写
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    


}

